/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.core.validate.AddGroup;
import com.comac.ins.common.idempotent.annotation.RepeatSubmit;
import com.comac.ins.common.log.annotation.Log;
import com.comac.ins.common.log.enums.BusinessType;
import com.comac.ins.common.mybatis.core.page.PageQuery;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.common.web.core.BaseController;
import com.comac.ins.isfd.domain.bo.*;
import com.comac.ins.isfd.domain.vo.IsfdDatabaseModelConfigCardVo;
import com.comac.ins.isfd.domain.vo.IsfdDatabaseModelConfigShowVo;
import com.comac.ins.isfd.domain.vo.IsfdDatabaseModelConfigTableVo;
import com.comac.ins.isfd.domain.vo.IsfdDatabaseModelConfigVo;
import com.comac.ins.isfd.service.IIsfdDatabaseModelConfigService;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据库建模配置
 *
 * @author lj
 * @date 2025-01-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/isfd/databaseModel/config")
public class IsfdDatabaseModelConfigController extends BaseController {

    private final IIsfdDatabaseModelConfigService isfdDatabaseModelConfigService;
    /**
     * 模型刷新
     */
    @SaCheckPermission("isfd:databaseModel:refresh")
    @GetMapping("/refresh")
    public R<Void> refresh() {
        return toAjax(isfdDatabaseModelConfigService.refresh());
    }

    /**
     * 数据采集卡片列表
     */
    @SaCheckPermission("isfd:databaseModel:config:list")
    @GetMapping("/card/list")
    public TableDataInfo<IsfdDatabaseModelConfigVo> cardList(@RequestParam(value = "pageNum",required = false) Integer pageNum,
                                                             @RequestParam(value = "pageSize",required = false) Integer pageSize,
                                                             @RequestParam(value = "modelName",required = false) String modelName) {
        return isfdDatabaseModelConfigService.collectCardList(pageNum, pageSize, modelName);
    }

    /**
     * 数据采集-点击任何卡片查询
     */
    @SaCheckPermission("isfd:databaseModel:config:detail")
    @GetMapping("/card/detail")
    public R<IsfdDatabaseModelConfigVo> collectCardDetail(@RequestParam("modelId") Long modelId) {
        return R.ok(isfdDatabaseModelConfigService.collectCardDetail(modelId));
    }

    /**
     * 查询数据库建模配置列表
     */
    @SaCheckPermission("isfd:databaseModel:config:list")
    @PostMapping("/list")
    public TableDataInfo<IsfdDatabaseModelConfigVo> list(@RequestBody IsfdDatabaseModelConfigQueryBo queryBo) {
        if (queryBo.getPageQuery().getPageSize() == null) {
            queryBo.setPageQuery(new PageQuery()); // 手动补充
            queryBo.getPageQuery().setPageNum(1);
            queryBo.getPageQuery().setPageSize(99);
        }
        return isfdDatabaseModelConfigService.queryPageList(queryBo);
    }

    /**
     * 查询数据库建模配置详情
     */
    @SaCheckPermission("isfd:databaseModel:config:detail")
    @GetMapping("/detail")
    public R<IsfdDatabaseModelConfigShowVo> detail(@RequestParam("id") Long id) {
        return R.ok(isfdDatabaseModelConfigService.queryById(id));
    }

    /**
     * 新增/修改数据库建模配置
     */
    @SaCheckPermission("isfd:databaseModel:config:add")
    @Log(title = "数据库建模配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/save")
    public R<Void> save(@Validated(AddGroup.class) @RequestBody IsfdDatabaseModelConfigSaveBo saveBo) {
        return toAjax(isfdDatabaseModelConfigService.saveConfig(saveBo, Boolean.FALSE));
    }

    /**
     * 升版数据库建模配置
     */
    @SaCheckPermission("isfd:databaseModel:config:upgrade")
    @Log(title = "数据库建模配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/upgrade")
    public R<Void> upgrade(@Validated(AddGroup.class) @RequestBody IsfdDatabaseModelConfigUpgradeBo upgradeBo) {
        return toAjax(isfdDatabaseModelConfigService.upgradeConfig(upgradeBo));
    }

    /**
     * 查询数据库建模字段类型列表
     */
    @SaCheckPermission("isfd:databaseModel:config:getFieldTypes")
    @GetMapping("/getFieldTypes")
    public R<List<String>> getFieldTypes() {
        return R.ok(isfdDatabaseModelConfigService.getFieldTypes());
    }

    /**
     * 获取数据库建模字段清单
     */
    @SaCheckPermission("isfd:databaseModel:config:getFieldList")
    @GetMapping("/getFieldList")
    public R<List<IsfdDatabaseModelConfigTableVo>> getFieldList(@RequestParam("modelId") Long modelId) {
        return R.ok(isfdDatabaseModelConfigService.getFieldTableList(modelId));
    }

    /**
     * 删除数据
     */
    @SaCheckPermission("isfd:databaseModel:config:delete")
    @Log(title = "删除配置数据", businessType = BusinessType.DELETE)
    @RepeatSubmit()
    @DeleteMapping("/{id}")
    public R<Void> remove(@NotNull(message = "主键不能为空")
                          @PathVariable Long id) {
        return toAjax(isfdDatabaseModelConfigService.deleteConfig(id));
    }

}
